package com.dpteam.dao.impl;

import com.dpteam.dao.OrderDao;
import com.dpteam.model.Order;
import com.dpteam.util.Page;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.List;

/**
 * Created by YangZhi on 2016/1/8.
 */
@Repository
public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao{

    private static DecimalFormat df=new java.text.DecimalFormat("#.##");

    @Override
    public List<Order> findByMemberId(Integer memberId, Page page) {
        String hql = "from Order o where o.member.id=?";
        return find(hql,page,memberId);
    }

    @Override
    public List<Order> findByUserId(Integer userId, Page page) {
        String hql = "from Order o where o.user.id=?";
        return find(hql,page,userId);
    }

    @Override
    public List<Order> findByTime(LocalDateTime fromTime, LocalDateTime toTime, Page page) {
        String hql = "from Order o where o.submitTime between ? and ?";
        return find(hql,page,new Object[]{fromTime,toTime});
    }

    @Override
    public List<Order> findByUserIdAndTime(Integer userId, LocalDateTime fromTime, LocalDateTime toTime, Page page) {
        String hql = "from Order o where o.user.id = ? and o.submitTime between ? and ?";
        return find(hql,page,new Object[]{userId,fromTime,toTime});
    }

    @Override
    public int deleteOrderById(Integer id) {
        Order order = get(Order.class,id);
        if(order!=null) {
            delete(order);
            return 1;
        }
        return 0;
    }

    @Override
    public List<Order> findByYear(int year) {
        LocalDate begin = LocalDate.of(year, 1, 1);
        LocalDate end = LocalDate.of(year, 12, 31);
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        return findBetween(beginTime, endTime);
    }

    @Override
    public List<Order> findBetween(LocalDateTime begin, LocalDateTime end) {
        String hql = "from Order where submitTime >= ? and submitTime <= ?";
        return find(hql, begin, end);
    }

    @Override
    public double sumProfit(int year) {
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, 1, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, 12, 31), LocalTime.MAX);
        String hql = "select sum(profitTotal) from Order where submitTime >= ? and submitTime <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumSell(int year) {
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, 1, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, 12, 31), LocalTime.MAX);
        String hql = "select sum(o.sellPriceTotal) from Order o where submitTime >= ? and submitTime <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumProfit(int year, int month) {
        Month month1 = Month.of(month);
        int maxDays = month1.maxLength();
        LocalDate localDate = LocalDate.of(year, month, 1);
        if (!localDate.isLeapYear() && month == 2) {
            maxDays--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDays), LocalTime.MAX);
        String hql = "select sum(profitTotal) from Order where submitTime >= ? and submitTime <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumSell(int year, int month) {
        Month month1 = Month.of(month);
        int maxDays = month1.maxLength();
        LocalDate localDate = LocalDate.of(year, month, 1);
        if (!localDate.isLeapYear() && month == 2) {
            maxDays--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDays), LocalTime.MAX);
        String hql = "select sum(o.sellPriceTotal) from Order o where submitTime >= ? and submitTime <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }
}
